package com.example.baron;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.jar.Attributes.Name;

public class GsonFragment extends Fragment {
	ListView postLV = null;
	Context context = null;
	ArrayAdapter<Post> adapter = null;
	LayoutInflater inflater;
	Post[] posts;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_gson, viewGroup,
				false);
		context = rootView.getContext();
		this.inflater = inflater;

		
		
		postLV = (ListView) rootView.findViewById(R.id.postLV);
	
		postLV.setEmptyView(rootView.findViewById(R.id.emptyTV));
		postLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> aadapterView, View view,
					int position, long id) {
				Post post = posts[position];
				toast(post.name);
			}
		});
		load();
		return rootView;
	}

	void load() {
		Gson gson = new Gson();

		BufferedReader reader = null;

		try {
			InputStream inputStream = context.getAssets().open("list.json");
			reader = new BufferedReader(new InputStreamReader(inputStream,
					"UTF-8"));
			posts = gson.fromJson(reader, Post[].class);
			adapter = new PostArrayAdapter(context, R.layout.row_post, posts);
			
			postLV.setAdapter(adapter);

		} catch (IOException e) {

			e.printStackTrace();
			toast("Error : IOException");
			
		} finally {
			try {
				if (reader != null)
					reader.close();
			} catch (IOException e) {
			}
		}
	}

	void toast(String message) {
		Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}

	class PostArrayAdapter extends ArrayAdapter<Post> {

		public PostArrayAdapter(Context context, int resource, Post[] posts) {
			super(context, resource);

		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			if (view == null) {
				view = inflater.inflate(R.layout.row_post, null );
			}
			Post post = getItem(position);

			ImageView profileIV = (ImageView) view.findViewById(R.id.profileIV);
			Picasso.with(context)
					.load("file:///android_asset/" + post.image_path)
					.resize(150, 100).centerCrop().into(profileIV);

			((TextView) view.findViewById(R.id.nameTV)).setText(post.name);
			((TextView) view.findViewById(R.id.descTV)).setText(post.desc);

			return view;
		}

	}
}
